class AssetsLoader {
  
  static int _loadsRemaining;
  
  static bool get finishedLoading() => (_loadsRemaining == 0);
  
  static List<String> get _IMAGES_TO_LOAD() => 
      ['img/spaceBG.gif', 
       'img/bullet.png', 
       'img/trollfaceSpaceship.png', 
       'img/shipAnimation.png',
       'img/explosion.png'];
                                
  
  static List<String> get _SOUNDS_TO_LOAD() => 
      ['sfx/lasershot.ogg', 
       'sfx/explosion.ogg'];
  
  static List<Sound> sounds;
  static List<ImageElement> images;
  
  static void loadSounds()
  {
    sounds = new List<Sound>();
    
    _SOUNDS_TO_LOAD.forEach((String src) => sounds.add(new Sound(src)));
  }
  
  static void loadImages()
  {
    images = new List<ImageElement>();
    _IMAGES_TO_LOAD.forEach((String src) => images.add(Sprite.getImage(src)));
    
  }
  
  static loadAll()
  {
    _loadsRemaining = _SOUNDS_TO_LOAD.length + _IMAGES_TO_LOAD.length;
    loadImages();
    loadSounds();
    
    window.webkitRequestAnimationFrame(updateLoadState);
  }
  
  static void updateLoadState(int time)
  {
    sounds.forEach((Sound s){
      if(s.state == "READY")
      {
        sounds.removeRange(sounds.indexOf(s), 1);
      }
    });
    
    images.forEach((ImageElement img){
      if(img.complete)
      {
        images.removeRange(images.indexOf(img), 1);
      }
    });
    
    _loadsRemaining = images.length + sounds.length;
    
    if(!finishedLoading)
    {
      window.webkitRequestAnimationFrame(updateLoadState);
    }
  }
}
